package com.vzoom.logdesensitization.autoconfig;

import com.vzoom.logdesensitization.config.DesensitizationConfig;
import com.vzoom.logdesensitization.config.DesensitizationStrategyHolder;
import com.vzoom.logdesensitization.mybatis.MybatisParameterLogInterceptor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomSqlSessionFactory implements SmartInitializingSingleton {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private DesensitizationConfig desensitizationConfig;

    @Autowired
    private DesensitizationStrategyHolder desensitizationStrategyHolder;

    @Override
    public void afterSingletonsInstantiated() {
        if (desensitizationConfig.getEnable()) {
            Configuration configuration = sqlSessionFactory.getConfiguration();
            configuration.addInterceptor(new MybatisParameterLogInterceptor(configuration,
                    desensitizationStrategyHolder.getStrategyMap(), desensitizationConfig.getSqlParameter()));
        }
    }
}
